<?php
/* 
* @Author: LDF QQ 47121862
* @Date:   2014-05-19 21:25:09
* @Last Modified by:   LDF QQ 47121862
* @Last Modified time: 2014-05-22 23:21:13
*/
class BrandModel extends viewModel{
	public $table = 'brand';
	public $view = array(
		'category' => array(
			'type' => INNER_JOIN,
			'on' => 'brand.cid=category.cid'
		)
	);
	public $validate = array(
		array('bname','nonull','品牌名称不能为空',2,3)
	);
	public $auto = array(
		array('logo','autoLogo','method',2,3)
	);
	//logo图片处理
	public function autoLogo(){
		if(!empty($_FILES['logo']['name'])){
			if(isset($_POST['pic'])) @unlink(ROOT_PATH.$_POST['pic']);
			$u = new Upload();
			$info = $u->upload('logo');
			return $info[0]['path'];
		}
	}
	//添加品牌
	public function addBrand(){
		if($this->create()){
			$bid=$this->add();
			if($bid){
				$this->updateCache();
				return $bid;
			}
			$this->error='添加品牌失败';
			return false;
		}else{
			return false;
		}
	}
	//修改品牌
	public function editBrand(){
		if($this->create()){
			$bid=Q('bid',0,'intval');
			$id=$this->save($bid);
			if($id){
				$this->updateCache();
				return $id;
			}
			$this->error='修改品牌失败';
			return false;
		}else{
			return false;
		}
	}
	//删除品牌
	public function delBrand(){
		$bid = Q('bid',0,'intval');
		$data=$this->find($bid);
		if($data['logo']) @unlink(ROOT_PATH.$data['logo']);
		if($this->del($bid)){
			$this->updateCache();
			return true;
		}else{
			return false;
		}
	}
	//更新缓存
	public function updateCache(){
		$brand = $this->order('bid DESC')->getField('bid,bname,logo,hd_g_brand.cid,gtid,cname');
		return cache('brandCache', $brand);
	}
}
